** This Do-File replicates Table A10 from P. Dutronc, A. Tondini "Large Means-Tested Pensions with Informal Labor Markets" ** 

set more off

cd "" /*Input here directory where the PALMS v3.2 (only years from 2002--2015) is stored */ 

use "south_africa_palms.dta", clear


**************************************
** Select Population for Estimation ** 
**************************************

drop if year==2008 | year==2009 /* Exclude years in which the threshold is lowered from 65 to 60 */ 

keep if pop_group==1 | pop_group==2  /* Keep Black and Coloured individuals only, see text for explanation */ 

drop if sex==. | sex==9 /* Exclude observations with missing value for gender */ 

gen married=(marital_status==1) /* 1/0 variable for marital status */ 


keep if age>=55 & age<65 /*Window +/- 5 */ 

** Adjust weights for multiple year estimation ** 

bys year: egen mean_weight=mean(weight) 
replace weight=weight/mean_weight /* Adjust weights to have the same mean across waves */

** Definition of Informal Employment **

**** Contract-based definition of informal Emplyoment **** 

gen tot_inf=1 if status==1 & no_written_contract==1 /*Employees w/o written contract */
replace tot_inf=0 if status==1 & no_written_contract==0  /*Employees w. written contract */
replace tot_inf=1 if status==2 & not_registered==1 /*Self-employed w/o registered business*/
replace tot_inf=0 if status==2 & not_registered==0 /*Self-employed w. registered business*/
replace tot_inf=0 if status==3 | status==4 /*Not Employed*/ 

**** Social-Security based definition of informal Emplyoment **** 

*gen tot_inf=1 if status==1 & no_soc_sec==1 /*Employees w/o social security contributions */
*replace tot_inf=0 if status==1 & no_soc_sec==0 /*Employees sw. social security contributions */
*replace tot_inf=1 if status==2 & not_registered==1 /*Self-employed w/o registered business*/
*replace tot_inf=0 if status==2 & not_registered==0 /*Self-employed w. registered business*/
*replace tot_inf=0 if status==3 | status==4 /*Not Employed*/ 


keep if tot_inf!=. /* Exclude from the estimation those with missing informality status.*/

gen f_emp=(tot_inf==0 & emp==1)


keep if tot_inf!=. /* Exclude if Informality Status is missing */

**  Wages and Hours ** 

replace hrs_wrk=0 if status==3 | status==4 /* Set hours equal to zero if not employed */
replace monthly_sal=0 if status==3 | status==4 /* Set monthly salary equal to zero if not employed */

gen hrs_wrk_f=hrs_wrk if f_emp==1 /* hours of work in formal employment */
replace hrs_wrk_f=0 if f_emp==0 /* hours of work in formal employment */

gen hourly_wage=monthly_sal/(hrs_wrk*4.5)

replace monthly_sal=ln(monthly_sal) 
replace hourly_wage=ln(hourly_wage)


drop if hourly_wage==. & emp==1 /* Exclude if salary or hours information is missing */ 


**************************************
************ Regressions ** **********
**************************************

keep if sex==1 

gen OAP_1=(age>=60) /* Indicator variable for being at or above the threshold */ 

gen x=age-60 /* center discontinuity at 0 */ 
gen x2=x^2 

gen post=(year>2008) 

gen yob=year-age  

egen cluster=group(yob pop_group) /* Cluster for standard errors by pop_group*cohort group to account for repeated observations over time in DiDisc estimation, see text for explanation */ 


gen window=1 if age>=55 & age<65 
replace window=2 if age>=56 & age<64 
replace window=3 if age>=57 & age<63 
replace window=4 if age>=58 & age<62
replace window=5 if age>=59 & age<61

label var emp "Employment"
label var tot_inf "Informal"
label var f_emp "Formal"

foreach outcome of varlist emp tot_inf f_emp {
preserve

local controls i.Province i.married i.education household_size
local f_form x 

forvalues v = 1 (1) 4 {
reg `outcome' i.pop_group i.year c.(`f_form') c.(`f_form')#1.OAP_1 c.(`f_form')#1.post c.(`f_form')#1.OAP_1#1.post OAP_1 1.OAP_1#1.post `controls'  [w=weight] if window>=`v'  , robust
parmest, label list(parm label estimate min* max*) level(95)  saving(window`v', replace)

}
reg `outcome' i.year OAP_1 1.OAP_1#1.post `controls'  [w=weight] if window==5  , robust
parmest, label list(parm label estimate min* max*) level(95)  saving(window5, replace)

local mylabel : variable label `outcome'

use window1, clear
gen window=1
forvalues v = 2 (1) 5 {
append using window`v' 
replace window=`v' if window==.
}

keep if parm=="1.OAP_1#1.post"
label variable estimate "regression coefficient"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"
label def window  1 "+/-5" 2 "+/-4" 3 "+/-3" 4 "+/-2" 5 "59 vs. 60"
label values window window

****************
** Figure A10  **
****************
* Note: For panel b of the Figure, change the definition of informal employment on line 40 to the social security based definition. 

twoway (scatter estimate window, mcolor(black) msize(small) ) || (line estimate window if window<=4, lcolor(black) )   ///
|| (rcap min95 max95 window, lcolor(black) )  || , yline(0, lwidth(medthick) lcolor(black)) graphregion( color(white) ) plotregion(  fcolor(white) ) xlabel(1 (1) 19, valuelabels angle(0)) xtitle("Bandwidth Size (Cohorts)") legend( order(1 3)lab(1 "Coef") lab(3 "95% CI")) ///
||, xlabel(1 (1) 5, angle(45)) ylabel(-0.15 (0.02) 0.08) title(" `mylabel' ")
graph copy `outcome', replace 	
	
restore 

}
graph combine emp tot_inf f_emp, cols(3) ycommon

forvalues v = 1 (1) 5 {
erase  window`v'.dta 
}
